
/*
 * Hlpay-Plus aggregate payment system. Copyright
 * (c) 2022-2023 Hlpay Team Copyright has the right of final interpretation.
 */

package com.hlkj.pay.util;

import java.util.*;
import java.util.stream.Collectors;

import org.springframework.util.StringUtils;

import com.hlkj.pay.app.transfer.PermissionDoToResp;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.admin.PermissionDO;
import com.hlkj.pay.infrastructure.model.admin.RolePermissionMappingDO;
import com.hlkj.pay.vo.admin.resp.menu.RoleDetailMenuResp;
import com.hlkj.pay.vo.admin.resp.permission.Meta;
import com.hlkj.pay.vo.admin.resp.permission.PermissionDetailResp;
import com.hlkj.pay.vo.admin.resp.permission.PermissionResp;

import cn.hutool.core.bean.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/09/07 16:26
 */
public class PermissionCoverUtils {

    public static List<RoleDetailMenuResp> permissionMenuList(List<PermissionDO> permissionList) {
        // 用于存储所有节点
        Map<String, RoleDetailMenuResp> nodeMap = new HashMap<>();
        // 存储根节点
        List<RoleDetailMenuResp> roots = new ArrayList<>();

        // 第一步：创建所有节点并放入map
        for (PermissionDO permissionDO : permissionList) {
            // String code = item.get("code");
            // String parentCode = item.get("parentCode");
            // String name = item.get("name");
            RoleDetailMenuResp node = BeanUtil.copyProperties(permissionDO, RoleDetailMenuResp.class);
            node.processMeta();
            Meta meta = node.getMeta();
            if (meta != null) {
                node.setModelName(meta.getTitle());
            }
            nodeMap.put(permissionDO.getCode(), node);
        }
        // 第二步：建立父子关系
        for (PermissionDO permissionDO : permissionList) {
            String code = permissionDO.getCode();
            String parentCode = permissionDO.getParentCode();
            RoleDetailMenuResp currentNode = nodeMap.get(code);
            // 如果parentCode为空或不存在于map中，则为根节点
            if (!StringUtils.hasText(parentCode) || !nodeMap.containsKey(parentCode)) {
                roots.add(currentNode);
            }
            else {
                // 添加到父节点的children列表
                RoleDetailMenuResp parentNode = nodeMap.get(parentCode);
                parentNode.getChildren().add(currentNode);
                Collections.sort(parentNode.getChildren());
            }
        }
        Collections.sort(roots);
        return roots;
    }

    /**
     * permissionMenuList
     *
     * @param permissionList
     * @param adminRoleFlag
     * @param rolePermissions
     * @return
     */
    public static List<RoleDetailMenuResp> permissionMenuList(List<PermissionDO> permissionList, Boolean adminRoleFlag, List<RolePermissionMappingDO> rolePermissions) {
        if (rolePermissions == null) {
            rolePermissions = new ArrayList<>();
        }
        List<String> permissionCodes = rolePermissions.stream().map(RolePermissionMappingDO::getPermissionCode).collect(Collectors.toList());
        if (adminRoleFlag == null) {
            adminRoleFlag = false;
        }
        // 用于存储所有节点
        Map<String, RoleDetailMenuResp> nodeMap = new HashMap<>();
        // 存储根节点
        List<RoleDetailMenuResp> roots = new ArrayList<>();

        // 第一步：创建所有节点并放入map
        for (PermissionDO permissionDO : permissionList) {
            // String code = item.get("code");
            // String parentCode = item.get("parentCode");
            // String name = item.get("name");
            RoleDetailMenuResp node = BeanUtil.copyProperties(permissionDO, RoleDetailMenuResp.class);
            node.processMeta();
            Meta meta = node.getMeta();
            if (meta != null) {
                node.setModelName(meta.getTitle());
            }
            nodeMap.put(permissionDO.getCode(), node);
        }
        // 第二步：建立父子关系
        for (PermissionDO permissionDO : permissionList) {
            if (AdminUserEnum.PERMISSION_TYPE.BUTTON.getCode().equals(permissionDO.getType())) {
                continue;
            }
            String code = permissionDO.getCode();
            String parentCode = permissionDO.getParentCode();
            RoleDetailMenuResp currentNode = nodeMap.get(code);
            boolean check = adminRoleFlag || nodeMap.containsKey(code);
            int checkValue = check ? CommonEnum.YES_NO_TYPE.YES.getCode() : CommonEnum.YES_NO_TYPE.NO.getCode();
            currentNode.setPermissionFlag(checkValue);
            // 如果parentCode为空或不存在于map中，则为根节点
            if (!StringUtils.hasText(parentCode) || !nodeMap.containsKey(parentCode)) {
                roots.add(currentNode);
            }
            else {
                // 添加到父节点的children列表
                RoleDetailMenuResp parentNode = nodeMap.get(parentCode);
                parentNode.getChildren().add(currentNode);
                Collections.sort(parentNode.getChildren());
            }
        }
        Collections.sort(roots);
        return roots;
    }

    static Integer groupingByKey(Integer actionType) {
        if (actionType == null) {
            return -1;
        }
        return actionType;
    }

    /**
     * permissionMenuList
     *
     * @param permissionList
     * @param adminRoleFlag
     * @param permissionCodes
     * @return
     */
    public static List<PermissionDetailResp> userPermissionList(List<PermissionDO> permissionList, Boolean adminRoleFlag, List<String> permissionCodes) {
        if (adminRoleFlag == null) {
            adminRoleFlag = false;
        }
        List<PermissionResp> list = new ArrayList<>();

        List<PermissionDO> collect = permissionList.stream().filter(permissionDO -> permissionDO.getType().equals(AdminUserEnum.PERMISSION_TYPE.BUTTON.getCode()))
                .collect(Collectors.toList());

        List<PermissionDetailResp> permissionDetailResps = PermissionDoToResp.INSTANCE.domainToRespList(collect);
        permissionDetailResps.stream().forEach(permissionDetailResp -> permissionDetailResp.processMeta());
        return permissionDetailResps;
    }
}
